﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtlity;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class PJSampleInService
    {
        #region 新增
        public int InsertInfo(PJSampleIn main)
        {
            string sql = "insert into PJSampleIns";
            sql += "(WorkParentId,SerialNum,OrderType,OrderPurpose,BusinessReason,Maktx,Num)";
            sql += " values(@WorkParentId,@SerialNum,@OrderType,@OrderPurpose,@BusinessReason,@Maktx,@Num)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",main.WorkParentId),
                new SqlParameter("@SerialNum",main.SerialNum),
                new SqlParameter("@OrderType",main.OrderType),
                new SqlParameter("@OrderPurpose",main.OrderPurpose),
                new SqlParameter("@BusinessReason",main.BusinessReason),
                new SqlParameter("@Maktx",main.Maktx),
                new SqlParameter("@Num",main.Num)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 更改
        public int UpdateInfo(PJSampleIn main)
        {
            string sql = "update PJSampleIns set";
            sql += " Maktx=@Maktx,Num=@Num where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",main.WorkParentId),
                new SqlParameter("@Maktx",main.Maktx),
                new SqlParameter("@Num",main.Num)
            };
            return SQLHelper.Update(sql, param);
        }

        public int UpdateSapBill(string sapBill, int workParentId)
        {
            string sql = "update PJSampleIns set SapBill=@SapBill";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@SapBill",sapBill),
                new SqlParameter("@WorkParentId",workParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        public int UpdateStatues(int workParentId)
        {
            string sql = "update PJSampleIns set Statues=@Statues";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@Statues","1"),
                new SqlParameter("@WorkParentId",workParentId)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 获取最大Id行的流水号
        /// </summary>
        /// <returns></returns>
        public string GetLastSerialNum(string orderType, string orderPurpose, string businessReason)
        {
            string sql = "select SerialNum from PJSampleIns";
            sql += " where Id=(select Max(Id) from PJSampleIns";
            sql += " where OrderType=@OrderType and OrderPurpose=@OrderPurpose and BusinessReason=@BusinessReason)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@OrderType",orderType),
                new SqlParameter("@OrderPurpose",orderPurpose),
                new SqlParameter("@BusinessReason",businessReason)
            };
            return Convert.ToString(SQLHelper.GetSingleResult(sql, param));
        }
        public PJSampleIn GetSampleIn(int workParentId)
        {
            string sql = "select * from PJSampleIns where WorkParentId=@WorkParentId";
            PJSampleIn objPJSampleIn = null;
            SqlDataReader objReader = SQLHelper.GetReader(sql,new SqlParameter[]{new SqlParameter("@WorkParentId",workParentId)});
            if(objReader.Read())
            {
                objPJSampleIn = new PJSampleIn()
                {
                    Id = Convert.ToInt32(objReader["Id"]),
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    SerialNum = objReader["SerialNum"].ToString(),
                    OrderType = objReader["OrderType"].ToString(),
                    OrderPurpose = objReader["OrderPurpose"].ToString(),
                    BusinessReason = objReader["BusinessReason"].ToString(),
                    Maktx = objReader["Maktx"].ToString(),
                    Num = Convert.ToInt32(objReader["Num"]),
                    SapBill = objReader["SapBill"].ToString(),
                    Statues = objReader["Statues"].ToString() == "0" ? "未同步" : "已同步"
                };
            }
            objReader.Close();
            return objPJSampleIn;
        }
        public int IsNoSampleIn(int workParentId)
        {
            string sql = "select count(*) from PJSampleIns where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId)
            };
            return Convert.ToInt32(SQLHelper.GetSingleResult(sql, param));
        }

        #endregion
    }
}
